package com.moxiao.yfjh.module.activity.controller.admin.activityenrollverificate;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.activity.controller.admin.activityPackage.vo.ActivityPackagePageReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.activityenrollverificate.vo.*;
import com.moxiao.yfjh.module.activity.convert.activityenrollverificate.ActivityEnrollVerificateConvert;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityInfo.ActivityInfoDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityPackage.ActivityPackageDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityenrollverificate.ActivityEnrollVerificateDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.merchantInfo.MerchantInfoDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.productInfo.ProductInfoDO;
import com.moxiao.yfjh.module.activity.service.activityInfo.ActivityInfoService;
import com.moxiao.yfjh.module.activity.service.activityPackage.ActivityPackageService;
import com.moxiao.yfjh.module.activity.service.activityenrollverificate.ActivityEnrollVerificateService;
import com.moxiao.yfjh.module.activity.service.merchantInfo.MerchantInfoService;
import com.moxiao.yfjh.module.activity.service.productInfo.ProductInfoService;
import com.moxiao.yfjh.module.blind.dal.dataobject.member.BlindMemberUserDO;
import com.moxiao.yfjh.module.blind.service.member.BlindMemberUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 相亲报名（活动）核销")
@RestController
@RequestMapping("/activity/enroll-verificate")
@Validated
public class ActivityEnrollVerificateController {

    @Resource
    private ActivityEnrollVerificateService enrollVerificateService;
    @Resource
    private ActivityPackageService packageService;
    @Resource
    private MerchantInfoService merchantInfoService;
    @Resource
    private ProductInfoService productInfoService;
    @Resource
    private BlindMemberUserService userService;
    @Resource
    private ActivityInfoService infoService;

    @PostMapping("/create")
    @Operation(summary = "创建相亲报名（活动）核销")
    public CommonResult<Long> createEnrollVerificate(@Valid @RequestBody ActivityEnrollVerificateCreateReqVO createReqVO) {
        return success(enrollVerificateService.createEnrollVerificate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲报名（活动）核销")
    public CommonResult<Boolean> updateEnrollVerificate(@Valid @RequestBody ActivityEnrollVerificateUpdateReqVO updateReqVO) {
        enrollVerificateService.updateEnrollVerificate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲报名（活动）核销")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteEnrollVerificate(@RequestParam("id") Long id) {
        enrollVerificateService.deleteEnrollVerificate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲报名（活动）核销")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ActivityEnrollVerificateRespVO> getEnrollVerificate(@RequestParam("id") Long id) {
        ActivityEnrollVerificateDO enrollVerificate = enrollVerificateService.getEnrollVerificate(id);
        ActivityEnrollVerificateRespVO vo = ActivityEnrollVerificateConvert.INSTANCE.convert(enrollVerificate);
        if (Objects.nonNull(vo)) {
            // 获得活动产品信息
            ActivityPackagePageReqVO pageReqVO = new ActivityPackagePageReqVO();
            pageReqVO.setActiveId(vo.getActivityId());
            pageReqVO.setProductId(vo.getActivityProductsId());
            ActivityPackageDO pageReqDo = packageService.getOne(pageReqVO);
            if (Objects.nonNull(pageReqDo)) {
                MerchantInfoDO merchantInfo = merchantInfoService.getMerchantInfo(pageReqDo.getMerchantId());
                if (Objects.nonNull(merchantInfo)) {
                    vo.setMerchantName(merchantInfo.getName());
                }
                // 商品相关信息
                ProductInfoDO productInfoDO = productInfoService.getProductInfo(vo.getActivityProductsId());
                if (Objects.nonNull(productInfoDO)) {
                    vo.setProductName(productInfoDO.getName());
                    vo.setSupplyPrice(productInfoDO.getSupplyPrice());
                    vo.setPlatformPrice(productInfoDO.getPlatformPrice());
                    vo.setProductIntroduct(productInfoDO.getProductIntroduct());
                }
                BlindMemberUserDO userDO = userService.getMemberUser(vo.getUserId());
                if (Objects.nonNull(userDO)) {
                    vo.setNickName(userDO.getNickname());
                }
                ActivityInfoDO activityInfoDO = infoService.getInfo(vo.getActivityId());
                if (Objects.nonNull(activityInfoDO)) {
                    vo.setActivityInfoName(activityInfoDO.getName());
                    vo.setActivityInfoTime(activityInfoDO.getActiveDate());
                }
            }
        }

        return success(vo);
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲报名（活动）核销列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<ActivityEnrollVerificateRespVO>> getEnrollVerificateList(@RequestParam("ids") Collection<Long> ids) {
        List<ActivityEnrollVerificateDO> list = enrollVerificateService.getEnrollVerificateList(ids);
        return success(ActivityEnrollVerificateConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲报名（活动）核销分页")
    public CommonResult<PageResult<ActivityEnrollVerificateRespVO>> getEnrollVerificatePage(@Valid ActivityEnrollVerificatePageReqVO pageVO) {
        if (Objects.nonNull(pageVO.getActivityId())) {
            ActivityInfoDO activityInfoDO = infoService.getInfo(pageVO.getActivityId());
            if (Objects.nonNull(activityInfoDO)) {
                // 如果是活动发起人则查询全部都额核销码
                if (Objects.equals(activityInfoDO.getHeadUserId(), pageVO.getUserId()) && Objects.equals(activityInfoDO.getCostShare(), 1)) {
                    pageVO.setUserId(null);
                }
            }
        }


        PageResult<ActivityEnrollVerificateDO> pageResult = enrollVerificateService.getEnrollVerificatePage(pageVO);
        PageResult<ActivityEnrollVerificateRespVO> page = ActivityEnrollVerificateConvert.INSTANCE.convertPage(pageResult);
        page.getList().forEach(item -> {
            // 获得活动产品信息
            ActivityPackagePageReqVO pageReqVO = new ActivityPackagePageReqVO();
            pageReqVO.setActiveId(item.getActivityId());
            pageReqVO.setProductId(item.getActivityProductsId());
            ActivityPackageDO pageReqDo = packageService.getOne(pageReqVO);
            if (Objects.nonNull(pageReqDo)) {
                MerchantInfoDO merchantInfo = merchantInfoService.getMerchantInfo(pageReqDo.getMerchantId());
                if (Objects.nonNull(merchantInfo)) {
                    item.setMerchantName(merchantInfo.getName());
                }
                // 商品相关信息
                ProductInfoDO productInfoDO = productInfoService.getProductInfo(item.getActivityProductsId());
                if (Objects.nonNull(productInfoDO)) {
                    item.setProductName(productInfoDO.getName());
                    item.setSupplyPrice(productInfoDO.getSupplyPrice());
                    item.setPlatformPrice(productInfoDO.getPlatformPrice());
                    item.setProductIntroduct(productInfoDO.getProductIntroduct());
                }
                BlindMemberUserDO userDO = userService.getMemberUser(item.getUserId());
                if (Objects.nonNull(userDO)) {
                    item.setNickName(userDO.getNickname());
                }
                ActivityInfoDO activityInfoDO = infoService.getInfo(item.getActivityId());
                if (Objects.nonNull(activityInfoDO)) {
                    item.setActivityInfoName(activityInfoDO.getName());
                    item.setActivityInfoTime(activityInfoDO.getActiveDate());
                }
            }
        });

        return success(page);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲报名（活动）核销 Excel")
    @OperateLog(type = EXPORT)
    public void exportEnrollVerificateExcel(@Valid ActivityEnrollVerificateExportReqVO exportReqVO,
                                            HttpServletResponse response) throws IOException {
        List<ActivityEnrollVerificateDO> list = enrollVerificateService.getEnrollVerificateList(exportReqVO);
        // 导出 Excel
        List<ActivityEnrollVerificateExcelVO> datas = ActivityEnrollVerificateConvert.INSTANCE.convertList02(list);
        datas.forEach(item -> {
            // 获得活动产品信息
            ActivityPackagePageReqVO pageReqVO = new ActivityPackagePageReqVO();
            pageReqVO.setActiveId(item.getActivityId());
            pageReqVO.setProductId(item.getActivityProductsId());
            ActivityPackageDO pageReqDo = packageService.getOne(pageReqVO);
            if (Objects.nonNull(pageReqDo)) {
                MerchantInfoDO merchantInfo = merchantInfoService.getMerchantInfo(pageReqDo.getMerchantId());
                if (Objects.nonNull(merchantInfo)) {
                    item.setMerchantName(merchantInfo.getName());
                }
                // 商品相关信息
                ProductInfoDO productInfoDO = productInfoService.getProductInfo(item.getActivityProductsId());
                if (Objects.nonNull(productInfoDO)) {
                    item.setProductName(productInfoDO.getName());
                    item.setSupplyPrice(productInfoDO.getSupplyPrice());
                    item.setPlatformPrice(productInfoDO.getPlatformPrice());
                }
                BlindMemberUserDO userDO = userService.getMemberUser(item.getUserId());
                if (Objects.nonNull(userDO)) {
                    item.setNickName(userDO.getNickname());
                }
                ActivityInfoDO activityInfoDO = infoService.getInfo(item.getActivityId());
                if (Objects.nonNull(activityInfoDO)) {
                    item.setActivityInfoName(activityInfoDO.getName());
                    item.setActivityInfoTime(activityInfoDO.getActiveDate());
                }
            }
        });

        ExcelUtils.write(response, "相亲报名（活动）核销.xls", "数据", ActivityEnrollVerificateExcelVO.class, datas);
    }

}
